(function(){
	var menuData = [
		{
			'name'			: '商家管理',
			'description'	: '对商家的数据进行管理',
			'ico'			: 'pencil',
			'url'			: '#',
			'son'			:
			[
				{
					'name'			: '商家管理',
					'description'	: '对商家的信息进行管理',
					'ico'			: 'pencil',
					'state'			: ['object',{'objectName':'seller'}]				
				},
				{
					'name'			: '优惠券管理',
					'description'	: '对优惠券进行增删改查等管理',
					'ico'			: 'pencil',
					'state'			: ['object',{'objectName':'coupon'}]			
				},
				{
					'name'			: '奖品管理',
					'description'	: '对奖品进行增删改查等管理',
					'ico'			: 'pencil',
					'state'			: ['object',{'objectName':'surprice'}]			
				}
			]
		},
		{
			'name'			: '消费者管理',
			'description'	: '',
			'ico'			: 'pencil',
			'url'			: '#',
			'son'			:
			[
				{
					'name'			: '消费者管理',
					'description'	: '对消费者的信息进行管理',
					'ico'			: 'pencil',
					'state'			: ['object',{'objectName':'consumer'}],

				},
				{
					'name'			: '消费者优惠券管理',
					'description'	: '对消费者与优惠券之间的关系进行管理',
					'ico'			: 'pencil',
					'state'			: ['object',{'objectName':'consumer_coupon'}]		
				},
				{
					'name'			: '消费者奖品管理',
					'description'	: '对奖品进行增删改查等管理',
					'ico'			: 'pencil',
					'state'			: ['object',{'objectName':'consumer_surprice'}]			
				}
			] 
		},
		{
			'name'			: '系统管理',
			'description'	: '',
			'ico'			: 'pencil',
			'url'			: '#',
			'son'			:
			[
				{
					'name'			: '用户管理',
					'description'	: '对用户进行管理',
					'ico'			: 'pencil',
					'state'			: ['object',{'objectName':'admin'}]		
				}
			]
		}
	];
 	


	var myApp = angular.module('myApp', ['lumx','ui.router','ngResource','object','angularFileUpload','ng.ueditor']);
	//http://ui.lumapps.com/
	//https://docs.angularjs.org/api/ngResource/service/$resource
	//http://angular-ui.github.io/ui-router/site/#/api

	myApp.config(function($stateProvider, $urlRouterProvider) {

	    // For any unmatched url, redirect to /
	    $urlRouterProvider.otherwise("/");
	   
	    // Now set up the states
		$stateProvider
			.state('defaul', {
	    		url: "/",
	    		templateUrl: "views/main/index.html"
	  		})
	  		.state('object', {
	    		url: "/object/:objectName",
	    		templateUrl: "views/object/object_list.html",
	    		controller:'ObjectController'
	  		});
  	});

  	myApp.factory('API', ['$resource', function($resource) {
		return $resource('/:object/:id', null,
    	{	
        	'update': 	{ method:'PUT' 	},
        	'query':  	{ method:'GET'  }
    	});
	}]);

	myApp.controller('GlobalController', [
        '$scope', '$http', '$state', '$rootScope', '$upload',function($scope,$http,$state,$rootScope,$upload){
        	
	    //注销
		function kremove( key ) {window.localStorage.removeItem( key ); }
	    $scope.logout = function(){
	      $http.get('user/logout').success(function(data){
	          if(!data.error ){
	              kremove("userName");
	              kremove("name");
	              window.location.href = '/';             
	          }else{
	              alert(data.message);
	          }
	      });
	    } 
    	$scope.stateGo = function(menuTwo){

    		$state.go(menuTwo.state[0],menuTwo.state[1]);
    		return false;
    	}

      function kset(key,value) {window.localStorage.setItem( key , value );}
      function kget( key ) { return window.localStorage.getItem( key );}
		  

        	$rootScope.$on('$stateChangeSuccess',
			function(event, toState, toParams, fromState, fromParams){

				angular.forEach(menuData, function(menuOne) {
					if(menuOne.son){
						angular.forEach(menuOne.son, function(menuTwo) {
							if(menuTwo.son){
								angular.forEach(menuTwo.son, function(menuThree) {
									if($state.is(menuThree.state[0],menuThree.state[1])){
							  			$scope.breadcrumbData = [menuOne,menuTwo,menuThree];
							  		}
								});
							}else{
						  		if($state.is(menuTwo.state[0],menuTwo.state[1])){
						  			$scope.breadcrumbData = [menuOne,menuTwo];
						  		}								
							}

					  	});
					}else{
						if($state.is(menuOne.state[0],menuOne.state[1])){
				  			$scope.breadcrumbData = [menuOne];
				  		}
					}
				});

				$scope.menuData = menuData;
			});
        }
    ]);

})();
